package cn;

/**
 * 斐波那契数列
 *
 * @author Mxb
 * @version 1.0
 * @date 2020/2/21 18:48
 */
public class Main {
    /**
     * 斐波那契数 fb(3) = fb(2)+fb(1);
     * 0 1 2 3 4 5 6 7 8
     * 0 1 1 2 3 5 8 13 21
     */
    public static int fib1(int n) {
        if (n <= 1) {
            return n;
        }
        return fib1(n - 1) + fib1(n - 2);
    }

    public static int fib2(int n) {
        if (n <= 1) {
            return n;
        }
        int first = 0;
        int second = 1;

        for (int i = 0; i < n - 1; i++) {
            int sum = first + second;
            first = second;
            second = sum;
        }
        return second;
    }

    public static void main(String[] args) {
        //System.out.println(fib2(8));
        //System.out.println("************************");
        Times.test("fib1", new Times.Task() {
            @Override
            public void execute() {
                System.out.println(fib1(32));
            }
        });
        Times.test("fib2", new Times.Task() {
            @Override
            public void execute() {
                System.out.println(fib2(32));
            }
        });
    }
}
